<?php
// 我的钱包
namespace app\shop\controller;

use think\Db;
use think\Request;
use think\Session;
use think\Controller;
use app\shop\model\Member as MemberModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\MemberWithdraw as MemberWithdrawModel;

class MemberWallet extends Controller
{

    /**
     * 充值页面
     * @return array|mixed
     */
    public function index()
    {
        // 获取会员ID 参数
        $memberId = Session::get('member_id');
        $condition = (Request::instance()->param('type', 0) == 0) ? [] : ['type_id' => Request::instance()->param('type')];
        // 获取会员信息 调用information控制器方法
        $result = (new Information)->personalInformation($memberId);

        // 筛选条件
        $data = (new MemberWithdrawModel())->where(['member_id' => $memberId, 'status' => 1])
            ->field('id,type_id,price,balance,create_time,order_number')->where($condition)->order('create_time desc')->paginate(12,false);
        foreach ($data as &$value) {
            $value['goods'] = Db::name('member_order_attached')->where('shop_order_number', $value['order_number'])->field('title,thumb_img')->find();
        }


        return $this->fetch('', [
            'mate_head'    => ' 我的钱包-明细',
            'revenue'      => $data,
            'data'         => $result,
        ]);
    }





    /**
     * 生成订单号
     */
    protected function createNumber()
    {
        //订单号码主体（YYYYMMDDHHIISSNNNNNNNN）
        $order_id_main = date('YmdHis') . rand(10000000, 99999999);
        //订单号码主体长度
        $order_id_len = strlen($order_id_main);
        $order_id_sum = 0;
        for ($i = 0; $i < $order_id_len; $i++) {
            $order_id_sum += (int)(substr($order_id_main, $i, 1));
        }
        //唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
        $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);

        return $order_id;
    }
}